﻿using System;
using System.Drawing;
using static System.Convert;

namespace InterLock.Equipmet
{
    /// <summary>
    /// 信号机
    /// </summary>
    public class Signal : Equipment
    {
        public override Rectangle Region
        {
            get
            {
                if ((Character & ToInt32("0X0004", 16)) != 0)//获取高柱区域
                {
                    return new Rectangle(eX - r + dz, eY - r, 2 * r, 2 * r);
                }

                return new Rectangle(eX - r, eY - r, 2 * r, 2 * r);
            }
        }

        private int dx = 0, dy = 0, dz = 0, r = 7;   //相对坐标参数
        private int secondSignal = 0;                //第二个灯位
        private int eX = 0, eY = 0;                  //绘图起点

        public override void Draw(Graphics g)
        {
            if ((Character & ToInt32("0X0010", 16)) != 0) //左向
            {
                dx = r + 1;
                dy = -16;
                dz = 6;

                //绘制名称
                g.DrawString(Name, new Font("宋体", 9, FontStyle.Bold), Brushes.White, X1, Y1 - 38);
            }
            else if ((Character & ToInt32("0X0020", 16)) != 0)  //右向
            {
                dx = -r;
                dy = 16;
                dz = -6;

                //绘制名称
                g.DrawString(Name, new Font("宋体", 9, FontStyle.Bold), Brushes.White, X1 - 8, Y1 + 30);
            }
            eX = X1 + dx;
            eY = Y1 + dy;
            SolidBrush mybrush = GetBrush();
            Pen mypen = Pens.White;

            if ((Character & ToInt32("0X0002", 16)) != 0)//列车信号机
            {
                DrawShuntingSinal(g, mybrush, mypen);
            }
            if ((Character & ToInt32("0X0001", 16)) != 0)//调车信号机
            {
                DrawRouteSignal(g, mybrush, mypen);
            }
        }

        /// <summary>
        /// 列车信号机
        /// </summary>
        /// <param name="g"></param>
        /// <param name="mybrush"></param>
        /// <param name="mypen"></param>
        private void DrawRouteSignal(Graphics g, SolidBrush mybrush, Pen mypen)
        {
            g.DrawLine(mypen, X1, eY - 7, X1, eY + 7);//画信号柱
            if ((Character & ToInt32("0X0004", 16)) != 0)//高柱
            {
                g.DrawLine(mypen, X1, eY - 1, X1 + dz, eY - 1);
                g.FillEllipse(mybrush, eX - r + dz, eY - r, 2 * r, 2 * r);
                g.DrawEllipse(mypen, eX - r + dz, eY - r, 2 * r, 2 * r);//画高柱信号机
            }
            else if ((Character & ToInt32("0X0008", 16)) != 0)//矮柱
            {
                g.FillEllipse(mybrush, eX - r, eY - r, 2 * r, 2 * r);
                g.DrawEllipse(mypen, eX - r, eY - r, 2 * r, 2 * r);      //画矮柱信号机
            }
        }

        /// <summary>
        /// 调车信号机
        /// </summary>
        /// <param name="g"></param>
        /// <param name="mybrush"></param>
        /// <param name="mypen"></param>
        private void DrawShuntingSinal(Graphics g, SolidBrush mybrush, Pen mypen)
        {
            g.DrawLine(mypen, X1, eY - 7, X1, eY + 7);//画信号柱
            if ((Character & ToInt32("0X0004", 16)) != 0)//高柱
            {
                g.DrawLine(mypen, X1, eY - 1, X1 + dz, eY - 1);
                g.FillEllipse(mybrush, eX - r + dz, eY - r, 2 * r, 2 * r);
                g.DrawEllipse(mypen, eX - r + dz, eY - r, 2 * r, 2 * r);//画高柱信号机
                if (SignlState == SignlType.cxj)
                {
                    g.FillEllipse(mybrush, eX - r + dz + secondSignal, eY - r, 2 * r, 2 * r);
                    g.DrawEllipse(mypen, eX - r + dz + secondSignal, eY - r, 2 * r, 2 * r);      //画第二个灯位
                }
            }
            else if ((Character & ToInt32("0X0008", 16)) != 0)//矮柱
            {
                g.FillEllipse(mybrush, eX - r, eY - r, 2 * r, 2 * r);
                g.DrawEllipse(mypen, eX - r, eY - r, 2 * r, 2 * r);      //画矮柱信号机
            }
        }

        /// <summary>
        /// 根据信号机状态获取画刷
        /// </summary>
        /// <returns></returns>
        private SolidBrush GetBrush()
        {
            switch (SignlState)
            {
                case SignlType.normal:
                    if ((Character & ToInt32("0X0002", 16)) != 0)
                    {
                        return new SolidBrush(Color.Red);
                    }
                    else
                    {
                        return new SolidBrush(Color.Blue);
                    }
                case SignlType.zxj:
                    return new SolidBrush(Color.Yellow);

                case SignlType.cxj:
                    return new SolidBrush(Color.Yellow);

                case SignlType.tg:
                    return new SolidBrush(Color.Green);

                case SignlType.dc:
                    return new SolidBrush(Color.White);
            }
            return null;
        }
    }
}
